﻿Partial Public Class WebForm_ManageCodeMapping
    Inherits BasePage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hiUID.Value = ActualUID
            hiDBName.Value = dbName
            Me.pnlADD.GroupingText = Me.GetLocalResourceObject("pnlAddTopic")
            Me.pnlList.GroupingText = Me.GetLocalResourceObject("pnlListTopic")
            Me.BindGV()
            Me.BindDDLTableName()
            Me.BindDDLColumnName()

        End If
    End Sub

#Region "FormView事件"
    Protected Sub ddlTableName_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.AddHeader(TryCast(fv.FindControl("ddlTableName"), DropDownList))
    End Sub

    Protected Sub ddlColumnName_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.AddHeader(TryCast(fv.FindControl("ddlColumnName"), DropDownList))
    End Sub

    Protected Sub ddlTableName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        TryCast(Me.fv.FindControl("ddlColumnName"), DropDownList).Items.Clear()
        Me.BindDDLColumnName()
        Me.BindGV()
    End Sub

    Protected Sub ddlColumnName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.BindGV()
    End Sub

    Protected Sub fv_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fv.ItemInserting
       Dim bbl As WorkFlow.BBL.CodeMappingBBL = New WorkFlow.BBL.CodeMappingBBL()
        If bbl.CodeMappingIsExist(TableName, ColumnName, Value, dbName) Then
            ShowMessage(Me.GetLocalResourceObject("DataExist"))
            e.Cancel = True
        End If
        bbl.InsertData(TableName, ColumnName, Value, Text, Active, ChnText, EngText, ActualUID, dbName)
        Me.BindGV()
    End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        Me.BindGV()
    End Sub
#End Region

#Region "GridView事件"

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        gv.SelectedIndex = -1
        gv.EditIndex = -1
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gv.RowCancelingEdit
        gv.EditIndex = -1
        Me.BindGV()
    End Sub
    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
        
        End If
    End Sub

    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        Dim bbl As WorkFlow.BBL.CodeMappingBBL = New WorkFlow.BBL.CodeMappingBBL()
        Dim gvID As String = gv.DataKeys(e.RowIndex).Item("ID").ToString()
        bbl.DeleteData(gvID, dbName)
        Me.BindGV()
    End Sub

    Protected Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv.RowEditing
        Me.gv.EditIndex = e.NewEditIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gv.RowUpdated
        Me.gv.EditIndex = -1
        Me.BindGV()
    End Sub



    Protected Sub gv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gv.RowUpdating
        Dim bbl As WorkFlow.BBL.CodeMappingBBL = New WorkFlow.BBL.CodeMappingBBL()



        Dim gvTableName As String = TryCast(gv.Rows(e.RowIndex).FindControl("lblTableName"), Label).Text
        Dim gvColumnName As String = TryCast(gv.Rows(e.RowIndex).FindControl("lblColumnName"), Label).Text
        Dim gvValue As String = TryCast(gv.Rows(e.RowIndex).FindControl("lblValue"), Label).Text
        Dim gvID As String = gv.DataKeys(e.RowIndex).Item("ID").ToString()
        '    Dim gvMOIValue As String = TryCast(gv.Rows(e.RowIndex).FindControl("txtMOIValue"), TextBox).Text
        Dim gvText As String = TryCast(gv.Rows(e.RowIndex).FindControl("txtText"), TextBox).Text
        Dim gvChnText As String = TryCast(gv.Rows(e.RowIndex).FindControl("txtChnText"), TextBox).Text
        Dim gvEngText As String = TryCast(gv.Rows(e.RowIndex).FindControl("txtEngText"), TextBox).Text
        Dim Active As String = IIf(TryCast(gv.Rows(e.RowIndex).FindControl("chkActive"), CheckBox).Checked = True, "Y", "N")
        If bbl.CodeMappingIDIsExist(gvTableName, gvColumnName, gvValue, gvID, dbName) Then
            ShowMessage(Me.GetLocalResourceObject("DataExist"))
            e.Cancel = True
            Exit Sub
        End If
        bbl.UpdateData(gvText, gvChnText, gvEngText, Active, gvID, dbName)
        e.Cancel = True
        Me.gv.EditIndex = -1
        Me.BindGV()
    End Sub

    Protected Sub ddlTableName_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim gvRow As GridViewRow = TryCast(TryCast(sender, DropDownList).Parent.Parent, GridViewRow)
        Dim bbl As WorkFlow.BBL.CodeMappingBBL = New WorkFlow.BBL.CodeMappingBBL()
        TryCast(gv.Rows(gvRow.RowIndex).FindControl("ddlColumnName"), DropDownList).DataSource = bbl.GetddlColumnNameItem(TryCast(sender, DropDownList).SelectedValue, dbName)
        TryCast(gv.Rows(gvRow.RowIndex).FindControl("ddlColumnName"), DropDownList).DataBind()

    End Sub
#End Region

End Class

Partial Class WebForm_ManageCodeMapping
#Region "繫結資料"
    Private Sub BindDDLTableName()
        Dim bbl As WorkFlow.BBL.CodeMappingBBL = New WorkFlow.BBL.CodeMappingBBL()
        TryCast(fv.FindControl("ddlTableName"), DropDownList).DataSource = bbl.GetddlTableNameItem(dbName)
        TryCast(fv.FindControl("ddlTableName"), DropDownList).DataBind()
    End Sub
    Private Sub BindDDLColumnName()
        Dim bbl As WorkFlow.BBL.CodeMappingBBL = New WorkFlow.BBL.CodeMappingBBL()
        TryCast(fv.FindControl("ddlColumnName"), DropDownList).DataSource = bbl.GetddlColumnNameItem(TableName, dbName)
        TryCast(fv.FindControl("ddlColumnName"), DropDownList).DataBind()
    End Sub
    Public Sub BindGV()
        Dim bbl As WorkFlow.BBL.CodeMappingBBL = New WorkFlow.BBL.CodeMappingBBL()
        gv.DataSource = bbl.GetData(TableName, ColumnName, dbName)
        gv.DataBind()
    End Sub
#End Region

#Region "方法"
    Private Sub AddHeader(ByVal ddlItem As DropDownList)
        ddlItem.Items.Insert(0, New ListItem(Me.GetLocalResourceObject("All"), "0"))
    End Sub
#End Region

#Region "屬性"
    Public ReadOnly Property TableName() As String
        Get
            Return TryCast(Me.fv.FindControl("ddlTableName"), DropDownList).SelectedValue
        End Get

    End Property
    Public ReadOnly Property ColumnName() As String
        Get
            Return TryCast(Me.fv.FindControl("ddlColumnName"), DropDownList).SelectedValue
        End Get

    End Property
    Public ReadOnly Property Value() As String
        Get
            Return TryCast(Me.fv.FindControl("txtValue"), TextBox).Text.Trim()
        End Get

    End Property

    Public ReadOnly Property Text() As String
        Get
            Return TryCast(Me.fv.FindControl("txtText"), TextBox).Text.Trim()
        End Get

    End Property
    Public ReadOnly Property ChnText() As String
        Get
            Return TryCast(Me.fv.FindControl("txtChnText"), TextBox).Text.Trim()
        End Get

    End Property
    Public ReadOnly Property EngText() As String
        Get
            Return TryCast(Me.fv.FindControl("txtEngText"), TextBox).Text.Trim()
        End Get

    End Property
    Public ReadOnly Property Sequence() As String
        Get
            Return TryCast(Me.fv.FindControl("txtInteger"), TextBox).Text.Trim()
        End Get

    End Property
    Public ReadOnly Property Active() As String
        Get
            Return IIf(TryCast(Me.fv.FindControl("chkActive"), CheckBox).Checked = True, "Y", "N")
        End Get
    End Property
#End Region
End Class
